from rest_framework.generics import ListCreateAPIView
from meiduo_admin.serializer import user_serializer
from meiduo_admin.my_paginate import MyPageNumberPagination
from users.models import User


class UserTableView(ListCreateAPIView):
    # queryset = User.objects.all()
    # serializer_class = UserTableSerializer
    pagination_class = MyPageNumberPagination

    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')

        if keyword:
            return User.objects.filter(username__contains=keyword)
        else:
            return User.objects.all()

    def get_serializer_class(self):
        if self.request.method == 'GET':
            return user_serializer.UserTableSerializer
        else:
            return user_serializer.UserAddSerializer